package com.mlz.mlzmall.handler;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;


import com.mlz.mlzmall.bean.CartInfo;
import com.mlz.mlzmall.ui.inter.HandlerGoBackUI;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/5/16.
 */
public class ShoppingCartHandler extends Handler {
    Context context;
    HandlerGoBackUI handlerGoBackUI;
    int warehouseType;
    boolean allSelectNSC,allSelectSELF;
    ArrayList<CartInfo.CartProductDto> dataNSC = new ArrayList<CartInfo.CartProductDto>();
    ArrayList<CartInfo.CartProductDto> dataSELF = new ArrayList<CartInfo.CartProductDto>();
    ArrayList<CartInfo.CartProductDto> dataALL = new ArrayList<CartInfo.CartProductDto>();

    CartInfo.CartTotalDto tolats;
    public ShoppingCartHandler(Context context, HandlerGoBackUI handlerGoBackUI) {

        this.context = context;
        this.handlerGoBackUI = handlerGoBackUI;
    }

    @Override
    public void handleMessage(Message msg) {

        super.handleMessage(msg);
        Bundle dataGet = msg.getData();
        ArrayList listRust = dataGet.getParcelableArrayList("list");
        ArrayList<CartInfo.CartProductDto> data = (ArrayList<CartInfo.CartProductDto>) listRust.get(0);
        tolats = (CartInfo.CartTotalDto) msg.obj;
        warehouseType = msg.arg1;
        if (data.size() == 0) {
            dataNSC.clear();
            dataSELF.clear();
            handlerGoBackUI.changeUI(dataNSC,dataSELF,tolats);
        } else {
            //清空数据缓存
            switch (warehouseType) {
                // 美篮子仓库
                case 21:
                    // 清除数据
                    dataNSC.clear();
                    dataSELF.clear();
                    for (CartInfo.CartProductDto d : data) {
                        if ("SELF".equals(d.getWarehouse().toString())) {
                            dataSELF.add(d);
                        }
                    }
                    handlerGoBackUI.changeUI(dataNSC,dataSELF,tolats);
                    break;

                // 海外仓库
                case 22:
                    // 清除数据
                    dataNSC.clear();
                    dataSELF.clear();
                    for (CartInfo.CartProductDto d : data) {
                        if ("NSC".equals(d.getWarehouse().toString())) {
                            dataNSC.add(d);
                        }
                    }
                    handlerGoBackUI.changeUI(dataNSC,dataSELF,tolats);
                    break;

                // 全部仓库
                case 0:
                    // 清除数据
                    dataNSC.clear();
                    dataSELF.clear();
                    dataALL.clear();
                    for (CartInfo.CartProductDto d : data) {
                        if ("NSC".equals(d.getWarehouse().toString())) {
                            dataNSC.add(d);
                        }
                        if ("SELF".equals(d.getWarehouse().toString())) {
                            dataSELF.add(d);
                        }
                    }
                    for (int i = 0; i < dataNSC.size() + dataSELF.size(); i++) {
                        if (i < dataNSC.size()) {
                            dataALL.add(i, dataNSC.get(i));
                        } else {
                            dataALL.add(i, dataSELF.get(i - dataNSC.size()));
                        }
                    }
                    handlerGoBackUI.changeUI(dataNSC,dataSELF,tolats);
                    break;
                default:
//
                    break;

            }

        }


    }
}
